/***
 * CaRComp Translator
 * 
 * Copyright (C) 2011 Taylor L. Riche
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 ***/

package translator.frontend;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import org.antlr.runtime.*;

import translator.grammar.*;

import translator.objects.*;

public class Translator {
	
	private static String NO_FILE_GIVEN = "No input filename given.";
	private static String TOO_MANY_ARGS = "Non-fatal warning, only two arguments accepted, others ignored.";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		System.out.println("CaRComp Translator, ver 0.5");
		System.out.println("Released under GNU GPL v2.0");
		System.out.println("Copyright (C) 2011 Taylor L. Riche");
		System.out.println();
		
		String filename = "sample.txt";
		String root = "/";
		if (args.length == 0) {
			usage(NO_FILE_GIVEN);
			System.exit(1);
		}
		else if (args.length == 1) {
			filename = args[0];
		}
		else if (args.length == 2) {
			filename = args[0];
			root = args[1];
		}
		else if (args.length > 2) {
			filename = args[0];
			root = args[1];
			usage(TOO_MANY_ARGS);
		}
		
		Algorithm alg = null;
		System.out.println("Processing input file: " + filename);
		try {
			CharStream cs = new ANTLRFileStream(filename);
			AppLexer lexer = new AppLexer(cs);
			CommonTokenStream tokens = new CommonTokenStream();
			tokens.setTokenSource(lexer);
			AppParser parser = new AppParser(tokens);
			alg = parser.algo();
		} catch (FileNotFoundException e){
			System.err.println("Input file " + filename + "not found.");
			e.printStackTrace();
			System.exit(2);
	    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RecognitionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PrintStream fo = null;
		String out_fn = root + Entity.convertCamelCase(alg.getName()) + ".py";
		try {
			fo = new PrintStream(new FileOutputStream(out_fn));
		} catch (FileNotFoundException e) {
			System.err.println("Output path does not exist.");
			e.printStackTrace();
			System.exit(2);
		}
		System.out.println("Generating code file: " + out_fn);
		alg.generateCode(fo);

	}
	
	private static void usage(String error_msg) {
		System.err.println(error_msg);
		usage();
	}
	
	private static void usage() {
		System.out.println("Usage:");
		System.out.println("\tjava -jar trans.jar INPUT_FILE (optional) OUTPUT_ROOT_PATH");
	}

}
